import { createStore } from 'vuex';
import createPersistedState from 'vuex-persistedstate';

// 引入快速访问变量
import getters from './getters';

// 引入多个模块
const moduleFiles = require.context('./modules', true, /\.js$/);
const modules = moduleFiles.keys().reduce((modules, modulePath) => {
  const storeName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1');
  modules[storeName] = moduleFiles(modulePath).default;
  return modules;
}, {});

// 创建store
const store = createStore({
  plugins: [
    createPersistedState({
      storage: window.localStorage,
      reducer: (state) => {
        return {
          user: state.user,
        };
      },
    }),
  ], // vuex持续保留指定数据，避免刷新丢失
  modules,
  getters,
});

export default store;
